#include "diskio.h"
#include "stm32f10x.h"
//#include "bsp_sdio_sdcard.h"
#include "flash.h"



#define FLASH_SECTOR_SIZE 	512					    
u16	    FLASH_SECTOR_COUNT= 2048;	
#define FLASH_BLOCK_SIZE   	8     	//每个BLOCK有8个扇区


//初始化磁盘
DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber (0..) */
)
{
	//初始化spi_flash
	//w25q64
	SPI_FLASH_Init();
	return RES_OK;
}  

//获得磁盘状态
DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber (0..) */
)
{ 
	return RES_OK;
} 

//读扇区
//drv:磁盘编号0~9
//*buff:数据接收缓冲首地址
//sector:扇区地址
//count:需要读取的扇区数
DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	UINT count		/* Number of sectors to read (1..128) */
)
{
	for(;count>0;count--)
	{
		SPI_Flash_Read(buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
		sector++;
		buff+=FLASH_SECTOR_SIZE;
	}
	
	return RES_OK;	   
}

//写扇区
//drv:磁盘编号0~9
//*buff:发送数据首地址
//sector:扇区地址
//count:需要写入的扇区数
#if _USE_WRITE
DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	UINT count			/* Number of sectors to write (1..128) */
)
{	
	for(;count>0;count--)
	{										    
		SPI_Flash_Write((u8*)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE);
		sector++;
		buff+=FLASH_SECTOR_SIZE;
	}
	return RES_OK; 
   
}
#endif


//其他表参数的获得
 //drv:磁盘编号0~9
 //ctrl:控制代码
 //*buff:发送/接收缓冲区指针
#if _USE_IOCTL
DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res;						  			     
	switch(cmd)
	{
		case CTRL_SYNC:
			res = RES_OK; 
			break;	 
		case GET_SECTOR_SIZE:
			*(WORD*)buff = FLASH_SECTOR_SIZE;
			res = RES_OK;
			break;	 
		case GET_BLOCK_SIZE:
			*(WORD*)buff = FLASH_BLOCK_SIZE;
			res = RES_OK;
			break;	 
		case GET_SECTOR_COUNT:
			*(DWORD*)buff = FLASH_SECTOR_COUNT;
			res = RES_OK;
			break;
		default:
			res = RES_PARERR;
			break;
	}
	
    return res;
}
#endif
//获得时间
//User defined function to give a current time to fatfs module      */
//31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */                                                                                                                                                                                                                                          
//15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */                                                                                                                                                                                                                                                
DWORD get_fattime (void)
{			
	return 0;
}			 
////动态分配内存
//void *ff_memalloc (UINT size)			
//{
//	return (void*)mymalloc(size);
//}
////释放内存
//void ff_memfree (void* mf)		 
//{
//	myfree(mf);
//}
